var cheerio = require('cheerio')
var superagent = require('superagent')
var url = require('url')
var async = require('async')
// var eventproxy = require('eventproxy')
// var ep = new eventproxy()
var fs = require('fs')
var mongoose = require('mongoose')
var mongourl = 'mongodb://127.0.0.1:27017'
require('superagent-proxy')(superagent);

var proxy = process.env.http_proxy || 'http://H46425Z6PY4A7JDD:EFAA7E0E1C10E6FE@proxy.abuyun.com:9020';
mongoose.Promise = global.Promise; 
mongoose.connect(mongourl,{useMongoClient:true});  
var Schema = mongoose.Schema;  
//骨架模版  
var movieSchema = new Schema({
    city:String
})  
var sanjiquerenSchema = new Schema({
    city:String
})
//模型  
var Movie = mongoose.model('Movie', movieSchema);  
var Sanjiqueren = mongoose.model('Sanjiqueren', sanjiquerenSchema);  
// 存储数据  










var a=require('./sanji.js')
// var a=[{'city':'01'},{'city':'011'},{'city':'011'},{'city':'0112'},{'city':'0113'},{'city':'0113113'},{'city':'011'},{'city':'0114'}]
function pusharr(item){
    var sanjiqueren = new Sanjiqueren(item)  
    sanjiqueren.save(function(err) {
        if (err) {  
            console.log('保存失败')  
            return;  
        }
        console.log('ok')
    })
}




var list=[]
for(var i=0;i<a.length;i++){
    var num=0
    for(var j=0;j<a.length;j++){
        if(a[j].city.indexOf(a[i].city)>-1){
            num++
        }
    }
    if(num==1){
        list.push(a[i])
        pusharr(a[i])
        console.log(list.length)
    }
}
console.log('kaishi')


fs.writeFile("sanji2.js",JSON.stringify(list), function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
});









// async.mapLimit(suoyousanji,1,function(city, callback){
//     console.log(city)
//     superagent.get(city)
//     .proxy(proxy)
//     .end(function(err1,res1){
//         if(res1){
//             var $$ = cheerio.load(res1.text)
//             console.log($$(".box.sidesubcat.t5 a").length)
//             $$(".box.sidesubcat.t5 a").each(function(){
//                 sanjis.push($$(this).attr('href'))
//                 var sanji = new Sanji({city:$$(this).attr('href')})  
//                 sanji.save(function(err) {
//                     if (err) {  
//                         console.log('保存失败')  
//                         return;  
//                     }else{
//                         console.log('ok')
//                     }              
//                 }) 
//             })
//             console.log(sanjis.length+'='+city)
//         }
//     })
//     setTimeout(function(){
//         callback(null,city)
//     },1000)
// },function(err2,result2){
//     setTimeout(function(){
//         console.log(result2)
//     },5000)
// })



